﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace RESP_IIWaterAssessment
{
    public partial class FarmAgroIndicatorForm : Form
    {
        public FarmAgroIndicatorForm()
        {
            InitializeComponent();
        }

        int regID, avpID, fhID = -1, cultID = -1, year = DateTime.Now.Year;

        tb_farmAgroIndicator agroIndicator;

        private void FarmAgroIndicatorForm_Load(object sender, EventArgs e)
        {
            cbRegion.SelectedIndexChanged -= cbRegion_SelectedIndexChanged;
            cbRegion.DataSource = UtilClass.FillCBRegions();
            cbRegion.DisplayMember = "Name";
            cbRegion.ValueMember = "ID";

            if (InformationClass.isAdmin)
            {
                cbRegion.SelectedIndex = -1;
                cbRegion.Text = string.Empty;
                cbRegion.SelectedIndexChanged += new EventHandler(cbRegion_SelectedIndexChanged);
            }
            else
            {
                cbRegion.Enabled = false;
                regID = InformationClass.regionID;
                cbRegion.SelectedIndexChanged += new EventHandler(cbRegion_SelectedIndexChanged);
                cbRegion.SelectedValue = regID;
                cbRegion_SelectedIndexChanged(cbRegion, null);
                cbAVP.Enabled = true;
                cbAVP.Text = string.Empty;
                //toolStrip1.Visible = false;
            }

            for (int i = 1991; i < 2100; i++)
            {
                cbYear.Items.Add(i);
            }
            cbYear.SelectedIndex = cbYear.FindStringExact(DateTime.Now.Year.ToString());

            cbCulture.SelectedIndexChanged -= cbCulture_SelectedIndexChanged;
            cbCulture.DataSource = UtilClass.GetCBCultureDataSourse();
            cbCulture.DisplayMember = "Name";
            cbCulture.ValueMember = "ID";
            cbCulture.SelectedIndex = -1;
            cbCulture.SelectedIndexChanged += new EventHandler(cbCulture_SelectedIndexChanged);
        }

        private void cbRegion_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cbRegion.SelectedIndex >= 0)
            {
                cbAVP.Enabled = true;
                regID = Convert.ToInt32(cbRegion.SelectedValue);
                cbAVP.SelectedIndexChanged -= cbAVP_SelectedIndexChanged;
                cbAVP.DataSource = UtilClass.FillAVP(regID);
                cbAVP.ValueMember = "ID";
                cbAVP.DisplayMember = "Name";
                cbAVP.SelectedIndex = -1;
                cbAVP.Text = string.Empty;
                cbFH.DataSource = null;
                cbAVP.SelectedIndexChanged += new EventHandler(cbAVP_SelectedIndexChanged);

            }
        }

        private void cbAVP_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cbAVP.SelectedIndex >= 0)
            {
                avpID = Convert.ToInt32(cbAVP.SelectedValue);
                cbFH.SelectedIndexChanged -= cbFH_SelectedIndexChanged;
                cbFH.Enabled = true;
                cbFH.DataSource = DBClass.Farms
                    .Where(wh => wh.AVPID == avpID)
                    .Select(se => new { ID = se.ID, Name = se.Name })
                    .OrderBy(o => o.Name)
                    .ToArray();
                cbFH.DisplayMember = "Name";
                cbFH.ValueMember = "ID";
                cbFH.SelectedIndex = -1;
                cbFH.Text = string.Empty;
                cbFH.SelectedIndexChanged += new EventHandler(cbFH_SelectedIndexChanged);
            }
        }

        private void cbFH_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cbFH.SelectedIndex >= 0)
            {
                fhID = Convert.ToInt32(cbFH.SelectedValue);
                if (cultID > 0 && fhID > 0)
                    ShowAgroIndicators(cultID, year, fhID);
            }
            else
                fhID = 0;
        }


        private void cbCulture_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cbCulture.SelectedIndex >= 0)
            {
                cultID = Convert.ToInt32(cbCulture.SelectedValue);
                if (cultID > 0 && fhID > 0)
                    ShowAgroIndicators(cultID, year, fhID);
            }
            else
                cultID = 0;
        }

        private void cbYear_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cbYear.SelectedIndex >= 0)
            {
                year = Convert.ToInt32(cbYear.Items[cbYear.SelectedIndex].ToString());
                if (cultID > 0 && fhID > 0)
                    ShowAgroIndicators(cultID, year, fhID);
            }
            else
                year = DateTime.Now.Year;
        }

        private void ShowAgroIndicators(int cultID, int year, int farmID)
        {
            agroIndicator = DBClass.FarmAgroIndicator
                .Where(wh => wh.CultureID == cultID && wh.Year == year && wh.FarmID == farmID).SingleOrDefault();

            if (agroIndicator != null)
            {
                seedCostTextBox.Text = agroIndicator.SeedCost.Value.ToString();
                seedQuantityTextBox.Text = agroIndicator.SeedQuantity.Value.ToString();
                fertilizerCostTextBox.Text = agroIndicator.FertilizerCost.Value.ToString();
                fertilizerQuantityTextBox.Text = agroIndicator.FertilizerQuantity.Value.ToString();
                pesticideCostTextBox.Text = agroIndicator.PesticideCost.Value.ToString();
                pesticideQuantityTextBox.Text = agroIndicator.PesticideQuantity.Value.ToString();
                mechWorkCostTextBox.Text = agroIndicator.MechWorkCost.Value.ToString();
                mechWorkQuantityTextBox.Text = agroIndicator.MechWorkQuantity.Value.ToString();
                handWorkCostTextBox.Text = agroIndicator.HandWorkCost.Value.ToString();
                handWorkQuantityTextBox.Text = agroIndicator.HandWorkQuantity.Value.ToString();
                transportCostTextBox.Text = agroIndicator.TransportCost.Value.ToString();
                transportQuantityTextBox.Text = agroIndicator.TransportQuantity.Value.ToString();
                waterCostTextBox.Text = agroIndicator.WaterCost.Value.ToString();
                waterQuantityTextBox.Text = agroIndicator.WaterQuantity.Value.ToString();
                agroProductionCostTextBox.Text = agroIndicator.AgroProductionCost.Value.ToString();
                harvestCostTextBox.Text = agroIndicator.HarvestCost.Value.ToString();
                harvestQuantityTextBox.Text = agroIndicator.HarvestQuantity.Value.ToString();
                taxTextBox.Text = agroIndicator.Tax.Value.ToString();
            }
            else
            {
                seedCostTextBox.Text = seedQuantityTextBox.Text = fertilizerCostTextBox.Text = fertilizerQuantityTextBox.Text =
                    pesticideCostTextBox.Text = pesticideQuantityTextBox.Text = mechWorkCostTextBox.Text = mechWorkQuantityTextBox.Text =
                    handWorkCostTextBox.Text = handWorkQuantityTextBox.Text = transportCostTextBox.Text = transportQuantityTextBox.Text =
                    waterCostTextBox.Text = waterQuantityTextBox.Text = agroProductionCostTextBox.Text = harvestCostTextBox.Text = harvestQuantityTextBox.Text =
                    taxTextBox.Text = string.Empty;
            }
        }

        private void buttonAccept_Click(object sender, EventArgs e)
        {
            if (fhID > 0 && cultID > 0)
            {
                decimal seedQ, seedC, ferQ, ferC, pesQ, pesC, mechQ, mechC, handQ, handC, transpQ, transpC, watQ, watC, agrPr, harQ, harC, tax;
                bool correct = seedCostTextBox.Text.GetDecimalOrDefault(out seedC);
                correct &= seedQuantityTextBox.Text.GetDecimalOrDefault(out seedQ);
                correct &= fertilizerCostTextBox.Text.GetDecimalOrDefault(out ferC);
                correct &= fertilizerQuantityTextBox.Text.GetDecimalOrDefault(out ferQ);
                correct &= pesticideCostTextBox.Text.GetDecimalOrDefault(out pesC);
                correct &= pesticideQuantityTextBox.Text.GetDecimalOrDefault(out pesQ);
                correct &= mechWorkCostTextBox.Text.GetDecimalOrDefault(out mechC);
                correct &= mechWorkQuantityTextBox.Text.GetDecimalOrDefault(out mechQ);
                correct &= handWorkCostTextBox.Text.GetDecimalOrDefault(out handC);
                correct &= handWorkQuantityTextBox.Text.GetDecimalOrDefault(out handQ);
                correct &= transportCostTextBox.Text.GetDecimalOrDefault(out transpC);
                correct &= transportQuantityTextBox.Text.GetDecimalOrDefault(out transpQ);
                correct &= waterCostTextBox.Text.GetDecimalOrDefault(out watC);
                correct &= waterQuantityTextBox.Text.GetDecimalOrDefault(out watQ);
                correct &= agroProductionCostTextBox.Text.GetDecimalOrDefault(out agrPr);
                correct &= harvestCostTextBox.Text.GetDecimalOrDefault(out harC);
                correct &= harvestQuantityTextBox.Text.GetDecimalOrDefault(out harQ);
                correct &= taxTextBox.Text.GetDecimalOrDefault(out tax);

                if (!correct)
                {
                    if (MessageBox.Show(msgClass.msgUncorrectData_Continue, msgClass.msgAchtung, MessageBoxButtons.YesNo) == DialogResult.No)
                        return;
                }

                if (agroIndicator != null)
                {
                    agroIndicator.SeedCost = seedC;
                    agroIndicator.SeedQuantity = seedQ;
                    agroIndicator.FertilizerCost = ferC;
                    agroIndicator.FertilizerQuantity = ferQ;
                    agroIndicator.PesticideCost = pesC;
                    agroIndicator.PesticideQuantity = pesQ;
                    agroIndicator.MechWorkCost = mechC;
                    agroIndicator.MechWorkQuantity = mechQ;
                    agroIndicator.HandWorkCost = handC;
                    agroIndicator.HandWorkQuantity = handQ;
                    agroIndicator.TransportCost = transpC;
                    agroIndicator.TransportQuantity = transpQ;
                    agroIndicator.WaterCost = watC;
                    agroIndicator.WaterQuantity = watQ;
                    agroIndicator.AgroProductionCost = agrPr;
                    agroIndicator.HarvestCost = harC;
                    agroIndicator.HarvestQuantity = harQ;
                    agroIndicator.Tax = tax;
                }
                else
                {
                    agroIndicator = new tb_farmAgroIndicator
                    {
                        CultureID = cultID,
                        Year = year,
                        FarmID = fhID,
                        SeedCost = seedC,
                        SeedQuantity = seedQ,
                        FertilizerCost = ferC,
                        FertilizerQuantity = ferQ,
                        PesticideCost = pesC,
                        PesticideQuantity = pesQ,
                        MechWorkCost = mechC,
                        MechWorkQuantity = mechQ,
                        HandWorkCost = handC,
                        HandWorkQuantity = handQ,
                        TransportCost = transpC,
                        TransportQuantity = transpQ,
                        WaterCost = watC,
                        WaterQuantity = watQ,
                        AgroProductionCost = agrPr,
                        HarvestCost = harC,
                        HarvestQuantity = harQ,
                        Tax = tax,
                    };
                    DBClass.FarmAgroIndicator.InsertOnSubmit(agroIndicator);
                }
                try
                {
                    DBClass.RESPContext.SubmitChanges();
                    MessageBox.Show(msgClass.msgSubmitChanges, msgClass.msgAchtung);
                }
                catch
                {
                    MessageBox.Show(msgClass.msgError, msgClass.msgAchtung);
                }
            }
        }

        private void buttonCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
